package HOT100;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author 是阿秋啊
 * @date 2022/09/24 16:45
 **/
public class meituan3 {
    static ArrayList<int[]> list = new ArrayList<>();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] arr = new int[n][m];
        for (int i = 0; i < arr.length; i++) {
            String s = sc.next();
            for (int j = 0; j < arr[0].length; j++) {
                arr[i][j] = Integer.parseInt(s.substring(j, j + 1));
            }
        }
        int T = sc.nextInt();
        for (int i = 0; i < T; i++) {
            int x = sc.nextInt() - 1;
            int y = sc.nextInt() - 1;
            int k = sc.nextInt();
            int[][] tmp  = new int[n][m];
            for (int j = 0; j < arr.length; j++) {
                for (int z = 0; z < arr[0].length; z++) {
                    tmp[j][z] = arr[j][z];
                }
            }
            help(arr, x, y, k);
            arr = tmp;
        }
        for (int[] ints : list) {
            for (int anInt : ints) {
                System.out.print(anInt + "  ");
            }
            System.out.println();
        }
    }

    private static void help(int[][] tmp, int x, int y, int k) {
        if (k == 0) {
            list.add(new int[]{x + 1, y + 1});
            return;
        }
        if (x + 1 < tmp.length && tmp[x + 1][y] != tmp[x][y]) {
            tmp[x][y] = tmp[x + 1][y];
            help(tmp, x + 1, y, --k);
        } else if (y + 1 < tmp[0].length && tmp[x][y + 1] != tmp[x][y]) {
            tmp[x][y] = tmp[x][y + 1];
            help(tmp, x, y + 1, --k);
        } else if (y - 1 >= 0 && tmp[x][y - 1] != tmp[x][y]) {
            tmp[x][y] = tmp[x][y - 1];
            help(tmp, x, y - 1, --k);
        } else if (x - 1 >= 0 && tmp[x - 1][y] != tmp[x][y]) {
            tmp[x][y] = tmp[x - 1][y];
            help(tmp, x - 1, y, --k);
        } else {
            list.add(new int[]{x + 1, y + 1});
        }
    }
}